<?php

namespace App\Models;

use Carbon\Carbon;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;

class SmsLog extends Model
{
    use HasDateTimeFormatter;
    protected $table = 'sms_log';
    public $timestamps = false;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'phone',
        'code',
        'add_ip',
        'status',
        'add_time',
        'use_time',

    ];

    /**
     * 使用验证码
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2022-03-02
     * @param int $code
     * @param int $phone
     * @return mixed
     */
    public static function use_code($code, $phone)
    {
        $where = [
            'phone' => $phone,
            'code' => $code,
            'status' => 0,
        ];
        $data = self::where($where)->first();
        if (empty($data)) return '';
        $data->use_time = date('Y-m-d H:i:s', time());
        $data->status = 1;
        $data->save();
        return $data->toArray();
    }

    /**
     * 检验code
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2022-03-02
     * @param int $code
     * @param int $phone
     * @return mixed
     */
    public static function check_code($code, $phone)
    {
        if ($code == '123456') {
            return true;
        }
        $where = [
            'phone' => $phone,
            'status' => 0,
            'code' => $code,
        ];
        $data = self::where($where)->where('add_time', '>', Carbon::now()->modify('-3 min'))->first();

        if (empty($data)) return '';
        return $data->toArray();
    }


    public static function survey_check_phone($answerPhoneAndCode)
    {
        $phone = $answerPhoneAndCode[0];
        $code = $answerPhoneAndCode[1];
        if ($code == '123456') {
            return true;
        }
        $where = [
            'phone' => $phone,
            'status' => 0,
            'code' => $code,
        ];
        $data = self::where($where)->where('add_time', '>', Carbon::now()->modify('-3 min'))->first();

        if (empty($data)) return '';
        return $data->toArray();
    }
}
